import pygame
from pygame.locals import*
import time,random

'''主函数'''
def main():
    '''创建窗口'''
    screen = pygame.display.set_mode((512,568),0,0)
    #创建背景图片
    background = pygame.image.load('D:/PythonProject/images/bg2.jpg')
    m= -968
    #创建本方飞机
    hero = HeroPlane(screen)
    enemy= EnemyPlane(screen)
    #存放敌机
    enemylist=[]
    while True:
        screen.blit(background,(0,m))
        m+=2
        if m>=-200:
            m= -968
        hero.display()
        #执行键盘控制
        key_control(hero)
        #随机绘制敌机
        if random.choice(range(50)) == 10:
            enemylist.append(EnemyPlane(screen))
        for em in enemylist:
            em.display()
            em.fire()
            if em.move(hero):
                enemylist.remove(em)
        # 更新显示
        pygame.display.update()
        time.sleep(0.05)
'''玩家飞机类'''
class HeroPlane(object):
    def __init__(self,screen_temp):
        #定义玩家初始位置与式样
        self.x=200
        self.y=480
        self.screen = screen_temp
        self.image = pygame.image.load('D:/PythonProject/images/me.png')
        self.bullet_list = [] #存储发射出去的子弹

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()
            if bullet.judge():
                #判断子弹是否越界
                self.bullet_list.remove(bullet)
    #设定左右移动距离与界限
    def move_left(self):
        self.x-=10
        if self.x<=0:
            self.x=0
    def move_right(self):
        self.x+=10
        if self.x>=406:
            self.x=406
    def fire(self):
        self.bullet_list.append(bullet(self.screen,self.x,self.y))
class  bullet(object):
    #hero发射子弹
    def __init__(self,screen_temp,x,y):
        #炮弹出现位置与式样
        self.x=x+53
        self.y=y-20
        self.screen=screen_temp
        self.image = pygame.image.load('D:\PythonProject\images/pd.png')

    def display(self):
        #绘制子弹
        self.screen.blit(self.image,(self.x,self.y))
    def move(self):
        self.y-=20
    def judge(self):
        if self.y<20:
            return True
        else:
            return False

def key_control(hero_temp):
    #获取按键事件
    for event in pygame.event.get():
        #判断是否点击退出按钮
        if event.type == QUIT:
            print("exit")
            exit()
    pressed_key = pygame.key.get_pressed()
    #判断按键
    if pressed_key[K_a] or pressed_key[K_LEFT]:
        print('left')
        hero_temp.move_left()
    elif pressed_key[K_d] or pressed_key[K_RIGHT]:
        print('right')
        hero_temp.move_right()
    elif pressed_key[K_SPACE]:
        print('space')
        hero_temp.fire()

class EnemyPlane(object):
    def __init__(self,screen_temp):
        #敌机出现位置与式样
        self.x=random.choice(range(408))
        self.y=-75
        self.screen = screen_temp
        #三种形式敌机轮流出现
        self.image=pygame.image.load('D:\PythonProject\images/e'+str(random.choice(range(3)))+'.png')
        self.bullet_list = []
    def display(self):
        #绘制敌机
        self.screen.blit(self.image,(self.x,self.y))
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()
            if bullet.judge():
                self.bullet_list.remove(bullet)
    def move(self,hero):
        self.y+=5
        if self.y>568:
            return True
        #英雄子弹与敌机碰撞检测及爆炸效果
        for bo in hero.bullet_list:
            if bo.x>self.x+12 and bo.x<self.x+92 and bo.y>self.y+20 and bo.y<self.y+60:
                hero.bullet_list.remove(bo)
                for i in range(0, 4):
                    im_path = 'D:/PythonProject/images/bo'+str(i)+'.png'
                    self.image=pygame.image.load((im_path))
                    self.screen.blit(self.image, (self.x, self.y))
                return True
            else:
                return False
    def fire(self):
        random_num = random.randint(1,80)
        if random_num == 10 or random_num==40:
            self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y))
class EnemyBullet(object):
    #敌机发射子弹
    def __init__(self,screen_temp,x,y):
        self.x=x+51
        self.y=y+60
        self.screen = screen_temp
        self.image = pygame.image.load('D:\PythonProject\images/pd2.png')
    def display(self):
        self.screen.blit(self.image, (self.x, self.y))
    def move(self):
        self.y+=10
    def judge(self):
        if self.y>852:
            return True
        else:
            return False
if __name__ =='__main__':
    main()
    
